package com.shengzhe.ostudent.dao.sysuser;

import com.shengzhe.ostudent.common.exception.OsException;
import com.shengzhe.ostudent.entity.Role;
import com.shengzhe.ostudent.entity.RoleCondition;
import com.shengzhe.ostudent.entity.SysUser;
import com.shengzhe.ostudent.entity.SysUserCondition;
import com.shengzhe.ostudent.utils.MD5Utils;
import com.shengzhe.ostudent.utils.OsBeanUtils;
import com.shengzhe.ostudent.utils.jdbc.JDBCUtils;
import com.shengzhe.ostudent.utils.jdbc.WhereSql;
import com.shengzhe.ostudent.utils.result.E;

import java.util.Date;
import java.util.List;

public class SysUserDaoImpl implements SysUserDao {
    @Override
    public boolean register(SysUser sysUser) {
        String sql = "insert into sysuser(phone,avatar,sex,realName,password,roleId,createTime,updateTime)" +
                "values(?,?,?,?,?,?,?,?)";
        Date date = new Date();
        String password = MD5Utils.encryptMD5(sysUser.getPassword());
        int result = JDBCUtils.executeUpdate(sql, sysUser.getPhone(), sysUser.getAvatar(), sysUser.getSex(), sysUser.getRealName(),
                password, sysUser.getRoleId(), date, date);
        return result > 0;
    }

    @Override
    public List<SysUser> list(SysUserCondition sysUserCondition) {
        String sql = "select * from sysuser";
        WhereSql where = WhereSql.buildAnd(sysUserCondition);
        return OsBeanUtils.resultSetToList(JDBCUtils.executeQuery(sql, where), SysUser.class);
    }

}